package com.lovi.product.controller;

import com.lovi.param.ProductHotParam;
import com.lovi.param.ProductIdsParam;
import com.lovi.param.ProductPromoParam;
import com.lovi.param.ProductSearchParam;
import com.lovi.product.service.ProductService;
import com.lovi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/promo")
    public R promo(@RequestBody @Validated ProductPromoParam productPromoParam, BindingResult result){

        if (result.hasErrors()){
            log.info("ProductController.promo 结果为->{}", "数据查询失败");

            return R.fail("数据查询失败!");
        }

        return  productService.promo(productPromoParam.getCategoryName());
    }

    @PostMapping("/test")
    public R promo2(){

        return R.ok("测试成功");
    }

    @PostMapping("/hots")
    public R hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){

        if (result.hasErrors()){
            return R.fail("数据查询失败!");
        }

        return productService.hots(productHotParam);
    }

    @PostMapping("/catetory/list")
    public R clist(){

        return productService.clist();
    }

    /**
     * 类别查询
     * @param productIdsParam
     * @return
     */
    @PostMapping("/bycategory")
    public Object byCategory(@RequestBody ProductIdsParam productIdsParam){

        return productService.byCategory(productIdsParam);
    }

    /**
     * 查询全部商品,可以复用业务!
     * @param productIdsParam
     * @return
     */
    @PostMapping("/all")
    public Object all(@RequestBody ProductIdsParam productIdsParam){

        return productService.byCategory(productIdsParam);
    }

    @PostMapping("/detail")
    public Object detail(@RequestBody Map<String,Integer> param){
        Integer productID = param.get("productID");
        return productService.detail(productID);
    }

    @PostMapping("/pictures")
    public Object productPictures(@RequestBody Map<String,Integer> param){
        Integer productID = param.get("productID");
        return productService.pictures(productID);
    }

    @PostMapping("/search")
    public Object search(@RequestBody ProductSearchParam productSearchParam){

        return productService.search(productSearchParam);
    }


}
